//
//  HICEmoticonPageView.swift
//  vibo
//
//  Created by jht-seven on 16/7/4.
//  Copyright © 2016年 jht-seven. All rights reserved.
//

import UIKit
// cell可重用标识符
private let HICEmoticonPageViewCellId = "HICEmoticonPageViewCellId"

class HICEmoticonPageView: UICollectionView {
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 注册cell
        registerClass(HICEmoticonPageViewCell.self, forCellWithReuseIdentifier: HICEmoticonPageViewCellId)
        // 设置代理
        dataSource = self
        // 设置layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = CGSize(width: SCREENW, height: 216 -  37)
        // 设置间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 设置滚动方向
        layout.scrollDirection = .Horizontal
    }
}

extension HICEmoticonPageView: UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return HICEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HICEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HICEmoticonPageViewCellId, forIndexPath: indexPath) as! HICEmoticonPageViewCell
        cell.indexPath = indexPath
        // 给cell赋值
        cell.emoticons = HICEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        
        return cell
    }
}
